package com.boyuan.huijiaoyu;

import java.util.ArrayList;

import com.example.contactfragment.ContactService;
import com.example.newfragment.DetailPerson;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.constant.Constant;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.ttcaca.cas.android.client.utils.Helper;

import android.os.Bundle;
import android.provider.SyncStateContract.Helpers;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class HomeContactDeail1Activity extends BaseActivity implements
		OnClickListener {

	private String yonghuID;
	private String uid;
	private ContactService contactService;
	private ImageView imag_touxiang;
	private DisplayImageOptions options;
	private TextView tv_titelback;
	private TextView tv_sex;
	private TextView tv_name;
	private ImageView image_sex;
	private DetailPerson homeContactDetailEntity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home_contact_deail1);
		initUI();
//		getContactDetail(uid);
	}

	private void initUI() {
		Intent intent = getIntent();
		yonghuID = intent.getStringExtra("yonghuID");
		uid = intent.getStringExtra("uid");
		String titleback = intent.getStringExtra("titleback");
		homeContactDetailEntity = (DetailPerson) intent.getSerializableExtra("homeContactDetailEntity");
		
		ImageLoader imageLoader = ImageLoader.getInstance();
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_titelback = (TextView) findViewById(R.id.tv_back_txt1);
		tv_titelback.setText(titleback);
		tv_sex = (TextView) findViewById(R.id.tv_sex);
		findViewById(R.id.btn_addcontact).setOnClickListener(this);
		findViewById(R.id.lin_back).setOnClickListener(this);
		imag_touxiang = (ImageView) findViewById(R.id.imageview);
		imag_touxiang.setOnClickListener(this);
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.default_header)
				.showImageForEmptyUri(R.drawable.default_header)
				.showImageOnFail(R.drawable.default_header).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true).build();
		image_sex = (ImageView) findViewById(R.id.iv_gender);
		
		tv_name.setText(homeContactDetailEntity.getRealname());
		String sex = homeContactDetailEntity.getSex();
		tv_sex.setText(sex);
		if("男".equals(sex)){
			image_sex.setBackgroundResource(R.drawable.img_male);
		}else{
			image_sex.setBackgroundResource(R.drawable.img_female);
		}
		String uri = homeContactDetailEntity.getAvatar();
		imageLoader.displayImage(uri, imag_touxiang, options);
		
	}
	

//	private void getContactDetail(String contactsID) {
//
//		if (contactService == null) {
//			contactService = new ContactService(getApplicationContext());
//		}
//		
//		showLoading(getString(R.string.loading_data));
//		
//		contactService.getContactdetail(yonghuID, uid,
//				new GetOneRecordListener<DetailPerson>() {
//
//					@Override
//					public void onFinish(DetailPerson data) {
//						hideLoading();
//						homeContactDetailEntity = data;
//						tv_name.setText(homeContactDetailEntity.getRealname());
//						String sex = homeContactDetailEntity.getSex();
//						tv_sex.setText(sex);
//						if("男".equals(sex)){
//							image_sex.setBackgroundResource(R.drawable.img_male);
//						}else{
//							image_sex.setBackgroundResource(R.drawable.img_female);
//						}
//						String uri = homeContactDetailEntity.getAvatar();
//						imageLoader.displayImage(uri, imag_touxiang, options);
//					}
//
//					@Override
//					public void onFailed(String Message) {
//						hideLoading();
//						ToastHelper.showMsg(HomeContactDeail1Activity.this,
//								Message, false);
//						finish();
//					}
//				});
//	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_addcontact:
			if(homeContactDetailEntity != null){
				addContact(yonghuID, uid);
			}else{
				Helper.showToast(HomeContactDeail1Activity.this, "获取联系人详情异常！");
			}
//			finish();
			break;
		case R.id.lin_back:
			finish();
			break;
		case R.id.imageview:
//			imag_touxiang.setDrawingCacheEnabled(true);
//			Bitmap bitmapCache = Bitmap.createBitmap(imag_touxiang.getDrawingCache());
//			imag_touxiang.setDrawingCacheEnabled(false);
//			Intent intent = new Intent(HomeContactDeail1Activity.this, ZoomActivity.class);
//			intent.putExtra("bitmap", bitmapCache);
//			startActivity(intent);
			String uri = Constant.photourl + uid;
			Intent imageIntent = new Intent(HomeContactDeail1Activity.this, ImageViewZoomActivity.class);
			imageIntent.putExtra("imageurl", uri);
			startActivity(imageIntent);
			break;
		default:
			break;
		}
	}

	private void addContact(String id1, String id2) {
		if (contactService == null) {
			contactService = new ContactService(getApplicationContext());
		}
		contactService.addContactFavorite(id1, id2,
				new GetOneRecordListener<String>() {

					@Override
					public void onFinish(String data) {
						Intent intent = new Intent();
						intent.setAction("Changyong");
						sendBroadcast(intent);
						Toast.makeText(HomeContactDeail1Activity.this, "添加成功！",
								Toast.LENGTH_LONG).show();
						Intent intent2 = new Intent(
								HomeContactDeail1Activity.this,
								HomeContactDeail2Activity.class);
						intent2.putExtra("homeContactDetailEntity", homeContactDetailEntity);
						intent2.putExtra("uid", uid);
						startActivity(intent2);
						finish();
					}

					@Override
					public void onFailed(String Message) {
						Toast.makeText(HomeContactDeail1Activity.this, Message,
								Toast.LENGTH_LONG).show();
					}
				});
	}

	@Override
	public void backClicked() {
		if (isShowingLoading()) {
			hideLoading();
		} else {
			setResult(RESULT_OK);
			isBackAllowed = true;
		}	}

}
